package com.sylar.happyreader.tools;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.http.protocol.HTTP;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class Utils {

	public static void LogD(String msg){
		if(Config.DEBUG)
		Log.d(Config.APPTAG, msg);
	}
	
	public static String[] SubArray(String[] args) {

		List<String> mlist = new ArrayList<String>(Arrays.asList(args));
		if (mlist != null && mlist.size() > 1) {
			mlist.remove(0);
			mlist.remove("李放");
			mlist.remove(mlist.size() - 1);
		}

		return (String[]) mlist.toArray(new String[mlist.size()]);
	}
	/**
	 * 判断一个字符串是否为一个http格式的url
	 * 
	 * @param input
	 *            要判断的字符串
	 * @return 返回结果，是返回true，否则返回false
	 */
	public static final boolean isStringBeHttpUrl(String input) {
		if (input != null) {
			if (input.startsWith("http://")) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 转换字节数组为utf8字符串
	 * 
	 * @param data
	 *            要转换的字节数组
	 * @return 返回的字符串，转换失败返回null
	 */
	public static final String byteToString(byte[] data) {
		String temp = null;
		if (data != null) {
			try {
				temp = new String(data, HTTP.UTF_8);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return temp;
	}

	public static String getSettingsparamsByString(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences("yibook", Context.MODE_PRIVATE);
		return settings.getString(key, "");
	}

	public static void setSettingsparamsByString(Context context, String key, String value) {
		SharedPreferences settings = context.getSharedPreferences("yibook", Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(key, value);
		editor.commit();
	}
	
	public static float getSettingsparamsByFloat(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences("yibook", Context.MODE_PRIVATE);
		return settings.getFloat(key, 0.5f);
	}

	public static void setSettingsparamsByFloat(Context context, String key, float value) {
		SharedPreferences settings = context.getSharedPreferences("yibook", Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putFloat(key, value);
		editor.commit();
	}
	
	public static int getSettingsparamsByInt(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences("yibook", Context.MODE_PRIVATE);
		return settings.getInt(key, -1);
	}

	public static void setSettingsparamsByInt(Context context, String key, int value) {
		SharedPreferences settings = context.getSharedPreferences("yibook", Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	public static boolean getSettingsparamsByBoolean(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences("yibook", Context.MODE_PRIVATE);
		return settings.getBoolean(key, false);
	}

	public static void setSettingsparamsByBoolean(Context context, String key, boolean value) {
		SharedPreferences settings = context.getSharedPreferences("yibook", Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidht = ((float) w / width);
		float scaleHeight = ((float) h / height);
		matrix.postScale(scaleWidht, scaleHeight);
		Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
		return newbmp;
	}


	public static Bitmap drawableToBitmap(Drawable drawable)
																
	{
		int width = drawable.getIntrinsicWidth(); 
		int height = drawable.getIntrinsicHeight();
		Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; // ȡdrawable����ɫ��ʽ
		Bitmap bitmap = Bitmap.createBitmap(width, height, config); 
		Canvas canvas = new Canvas(bitmap); 
		drawable.setBounds(0, 0, width, height);
		drawable.draw(canvas); 
		return bitmap;
	}

	public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
		int width = drawable.getIntrinsicWidth();
		int height = drawable.getIntrinsicHeight();
		Bitmap oldbmp = drawableToBitmap(drawable); 

		Matrix matrix = new Matrix(); 
		float scaleWidth = ((float) w / width);
		float scaleHeight = ((float) h / height);
		matrix.postScale(scaleWidth, scaleHeight); 
		Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true); 
		return new BitmapDrawable(newbmp); 
	}
}
